package com.lancoo.znbkxx.base;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.lancoo.znbkxx.views.AppLoadingDialog;

/**
 * Create by TangJun on 2019/2/16
 */
public abstract class BaseActivity extends AppCompatActivity {
    private Toast mToast = null;
    protected boolean IsClear = false;
    protected Boolean IsActivityDestroyed = false;
    private AppLoadingDialog loadingDialog;
    protected Handler mHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppActivityManager.getInstance().addActivity(this);
        mHandler = new Handler();
    }

    @Override
    public void finish() {
        super.finish();
        if (!IsClear) {
            IsClear = true;
            clear();
        }
    }

    protected void clear() {
        IsActivityDestroyed = true;
        AppActivityManager.getInstance().removeActivity(this);
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
        loadingDialog = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!IsClear) {
            IsClear = true;
            clear();
        }
    }

    public Boolean IsActivityDestroyed() {
        return IsActivityDestroyed;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    protected void replaceFragment(int containerViewId, Fragment fragment) {
        if (!IsActivityDestroyed()) {
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(containerViewId, fragment);
            transaction.commit();
        }
    }

    public void switchFragment(int containerViewId, Fragment from, Fragment to) {
        FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
        if (from != to) {
            if (!to.isAdded()) {
                mTransaction.hide(from).add(containerViewId, to);
            } else
                mTransaction.hide(from).show(to);

            mTransaction.commit();
        }

    }

    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (msg != null) {
                    if (mToast != null) {
                        mToast.cancel();
                    }
                    mToast = Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT);
                    mToast.show();
                }
            }
        });

    }

    protected final void Gone(View... views) {
        for (View v : views) {
            if (v != null) {
                v.setVisibility(View.GONE);
            }
        }

    }

    protected final void Show(View... views) {
        for (View v : views) {
            if (v != null) {
                v.setVisibility(View.VISIBLE);
            }
        }

    }

    protected final void Hide(View... views) {
        for (View v : views) {
            if (v != null) {
                v.setVisibility(View.INVISIBLE);
            }
        }

    }

    protected void showLoadingDialog() {
        getLoadingDialog().show();
    }

    protected AppLoadingDialog getLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new AppLoadingDialog(this, true);
        }
        return loadingDialog;
    }


    protected void hideLoadingDialog() {
        if (loadingDialog != null) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    loadingDialog.dismiss();
                }
            });

        }
    }
}
